function myAjax(url, data = {}, type = 'get') {
    return new Promise((resolve, reject) => {
        $.ajax({
            type,
            url,
            data,
            dataType: 'json',
            success: function (response) {
                resolve(response);
            },
            error(e) {
                console.log(e);
            }
        });
    })
}

// 获取首页数据
myAjax('http://47.97.121.9:3300/getRecommend')
    .then(res => {
        let forYou = res.response.recomPlaylist.data.v_hot;
        let newSong = res.response.new_song.data;
        let xdsf = res.response.new_album.data.albums;
        xrRecommend(forYou);
        xrNewSongs(newSong);
        renderXdsf(xdsf);
    })


// 渲染 为你推荐 歌单
function xrRecommend(data) {
    data.forEach((v, k) => {
        $('.gdtj .list_item_img>ul').append(`
        <li>
            <a href="/qqmusic2.0/qqmusic/player/player.html?disstid=${v.content_id}"><img src="${v.cover}" alt=""><span><i class="iconfont">&#xe609;</i></span></a>
            <h3><a href="javascript:;">${v.title}</a></h3>
            <span>播放量：${transPlayCount(v.listen_num)}</span>
        </li>`)
    })
}

// 渲染新歌首发
function xrNewSongs(data) {
    for (let i = 0; i <= parseInt(data.songlist.length / 9); i++) {
        $('.list_item_xgsf>.slide_list').append('<ul class="slide_list_item"></ul>');
    }

    data.songlist.forEach((v, k) => {
        if (k < 9) {
            renderXgsf(v, k, 0);
        } else if (k >= 9 && k < 18) {
            renderXgsf(v, k, 1);
        } else if (k >= 18 && k < 27) {
            renderXgsf(v, k, 2);
        } else if (k >= 27 && k < 36) {
            renderXgsf(v, k, 3);
        } else if (k >= 36 && k < 45) {
            renderXgsf(v, k, 4);
        } else if (k >= 45 && k < 54) {
            renderXgsf(v, k, 5);
        }
    })

}

function renderXgsf(v, k, i) {
    $('.slide_list_item').eq(i).append(`
                <li>
                    <div class="songs_img">
                        <a href="javascript:;"><img
                            src="https://y.qq.com/music/photo_new/T002R300x300M000${v.album.pmid}.jpg" alt=""></a>
                        <div class="songs_img_hide"><i class="iconfont">&#xe609;</i></div>
                    </div>
                    <div class="songs_info">
                        <div>
                            <h3><a href="javascript:;">${v.name}</a></h3>
                            <h4><a href="javascript:;" title="${v.singer[0].title}">${v.singer[0].name}</a></h4>
                        </div>
                    <span>03:10</span>
                    </div>
                </li>
        `)
}


// 渲染新碟首发
function renderXdsf(data) {
    data.forEach((v, k) => {
        $('.list_item_xdsf >ul').append(`
            <li>
                <a href="javascript:;">
                    <img src="https://y.qq.com/music/photo_new/T002R300x300M000${v.mid}.jpg?max_age=2592000" alt="">
                    <span><i class="iconfont">&#xe609;</i></span>
                </a>
                <h3>
                    <a href="javascript:;">${v.name}</a>
                </h3>
                <span><a href="javascript:;">${v.singers[0].name}</a></span>
            </li>
        `);
    })
}


// 播放量转换为 w 为单位
function transPlayCount(x) {
    if (x < 10000) { return x }
    else { return (`${parseFloat(Math.floor(x / 1000)) / 10}万`) }
}